package business_service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import dao_pool.Pool;

import Protocol.contact.Contact;
import Protocol.contact.ContactGroup;
import Protocol.user.BasePersonInfo;
import Protocol.user.Login_Q;
import Protocol.user.Login_R;
import Protocol.user.Logout_Q;
import Protocol.user.Logout_R;
import Protocol.utility.InitContent;
import i_business.I_Loginout;

public class LoginoutService implements I_Loginout {
	private 	Pool	m_Pool = Pool.getInstance();
	
	@Override
	public Login_Q doLogin(Login_R login) {
		// TODO Auto-generated method stub
		String name=login.getStudentno();
		String passwords=login.getPasswd();
		Login_Q a= new Login_Q();
		
		try{
			Connection conn=m_Pool.getConnection();
			if(!conn.isClosed())
				System.out.println(name+"\n"+passwords+"\n"+"successed connect to the Datebase");
			
			Statement statement=conn.createStatement();
			String sql="select*from user where user_number='"+name+"'";
			ResultSet rs = statement.executeQuery(sql);
			if(rs.next()) {
				if(rs.getString(3).equals(passwords)){
					a.setLogin_result("success");
					System.out.println(rs.getString(3));
				}
				else{
					a.setLogin_result("failure");
					//rs.close();
					return a;
				}
			}
			//rs.close();
			//statement.close();
			//Statement   s = conn.createStatement();
			
			sql = "select * from base_info where user_id ="+rs.getInt(1);
			ResultSet  rs2 = statement.executeQuery(sql);
			if(rs2.next()) {
				BasePersonInfo	clientBaseInfo = new BasePersonInfo();
				clientBaseInfo.setUser_id(rs2.getInt("user_id"));
				clientBaseInfo.setName(rs2.getString("user_name"));
				clientBaseInfo.setClss(rs2.getString("user_class"));
				//  todo here
				clientBaseInfo.setGender(rs2.getInt("user_gender"));
				a.setClientBaseInfo(clientBaseInfo);
			}
			sql = "select * from base_info";
			rs2 = statement.executeQuery(sql);
			Contact       contact = new Contact();
			ArrayList<BasePersonInfo>	 bpiList = new ArrayList<BasePersonInfo>();	
			ArrayList<ContactGroup>		groupList = new ArrayList<ContactGroup>();
			ContactGroup defaultGroup = new  ContactGroup(); 
			BasePersonInfo	tempBpi = null;
			while(rs2.next()) {
				tempBpi= new BasePersonInfo();
				tempBpi.setUser_id(rs2.getInt(1));
				tempBpi.setName(rs2.getString(2));
				//tempBpi.setType(rs2.getInt(2));
				tempBpi.setGender(rs2.getInt(3));
				bpiList.add(tempBpi);
				tempBpi = null;
			}
					
				defaultGroup.setGroupDescriptor("default group");
				defaultGroup.setGroupID(1);
				defaultGroup.setGroupName("default");
				defaultGroup.setGroupOwnerId(0);
				//defaultGroup.setMemberIdList(bpiList);
				
			groupList.add(defaultGroup);
			contact.setGroupList(groupList);
			
			
			a.setContact(contact);
			
		
			rs.close();
			rs2.close();
			statement.close();
			m_Pool.freeConnection(conn);
		}
		catch(SQLException e2){
			e2.printStackTrace();
		}
		catch(Exception e3){
			e3.printStackTrace();
		}
		return a;
		//return null;
	}

	@Override
	public boolean sendPhoto(int user_id) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean sendInitContent(InitContent ic) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Logout_Q doLogout(Logout_R ogout) {
		// TODO Auto-generated method stub
		return null;
	}

}
